package com.fuhao.member.service;

import com.alibaba.fastjson.JSONObject;
import com.fuhao.core.Constants.Constants;
import com.fuhao.core.base.BaseApiService;
import com.fuhao.core.base.BaseResponse;
import com.fuhao.core.transaction.RedisDataSoureceTransaction;
import com.fuhao.core.utils.GenerateToken;
import com.fuhao.core.utils.MD5Util;
import com.fuhao.core.utils.RegexUtils;
import com.fuhao.dto.input.MemberLoginInpDTO;
import com.fuhao.member.entity.MemberEntity;
import com.fuhao.member.entity.MemberTokenEntity;
import com.fuhao.member.mapper.UserMapper;
import com.fuhao.member.mapper.UserTokenMapper;
import com.fuhao.member.utils.TokenUniqueUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RestController;
import sun.security.util.ManifestEntryVerifier;

/**
 * @author zhang
 * @title: 用户登录
 */
@RestController
public class MemberLoginServiceImpl extends BaseApiService<JSONObject> implements MemberLoginService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GenerateToken generateToken;
    @Autowired
    private UserTokenMapper userTokenMapper;
    @Autowired
    private RedisDataSoureceTransaction dataSoureceTransaction;
    @Autowired
    private TokenUniqueUtils tokenUniqueUtils;

    @Override
    public BaseResponse<JSONObject> login(MemberLoginInpDTO memberLoginInpDTO) {
        //1.校验参数
        String mobile = memberLoginInpDTO.getMobile();
        if (StringUtils.isEmpty(mobile) && !RegexUtils.checkPhone(mobile)) {
            return setResultError("账号输入错误！");
        }
        String password = memberLoginInpDTO.getPassword();
        if (StringUtils.isEmpty(password)) {
            return setResultError("密码不能为空!");
        }
        String loginType = memberLoginInpDTO.getLoginType();
        if (StringUtils.isEmpty(loginType)) {
            return setResultError("登陆类型不能为空!");
        }
        //登录类型仅限于PC、IOS、Android
        if (!(loginType.equals(Constants.MEMBER_LOGIN_TYPE_ANDROID) || loginType.equals(Constants.MEMBER_LOGIN_TYPE_IOS)
                || loginType.equals(Constants.MEMBER_LOGIN_TYPE_PC))) {
            return setResultError("登陆类型出现错误!");
        }
        // 设备信息
        String deviceInfor = memberLoginInpDTO.getDeviceInfor();
        if (StringUtils.isEmpty(deviceInfor)) {
            return setResultError("设备信息不能为空!");
        }
        //2.比对登录账号密码
        String newPassword = MD5Util.MD5(password);
        MemberEntity memberEntity = userMapper.login(mobile, newPassword);
        if (memberEntity == null) {
            return setResultError("用户不存在！");
        }
        //若腾讯的openId不为空，表示qq登录，更新到用户表中
        String qqOpenId = memberLoginInpDTO.getQqOpenId();
        Long userid = memberEntity.getUserid();
        if(!StringUtils.isEmpty(qqOpenId)){
            userMapper.updateUserOpenId(qqOpenId,userid);
        }
        return tokenUniqueUtils.CheckTokenUnique(memberEntity,memberLoginInpDTO);
    }
}